
import 'dart:convert';
import 'package:get/get.dart';
import 'package:xin_he/page/login/xin_he_login/xin_he_login_view.dart';

import '../../service/xin_he_configure.dart';
import '../../utils/xin_he_event_bus.dart';
import '../../utils/xin_he_storage.dart';
import '../../utils/xin_he_user_data.dart';

class XinHeLoginAction  {

  //登录和注册完成后对一些数据需要进行设置
  static void loginSucSetData(Map data) async {
    //token写入本地
    await XinHeLocalStorage.set(XinHeStorage.accessToken, data['token']);
    await XinHeLocalStorage.set(XinHeStorage.userId, data['member']['fUserId']);

    //拿到token后，将token设置到请求头中
    Map<String, dynamic> heardMap = {
      'x-access-token' : data['token'] ?? '',
    };
    //设置请求头token等
    await httpRequestManager.setXinHeDioBaseOptionsParam(headers: heardMap);

    //存储用户数据
    // await XinHeLocalStorage.setJSON(XinHeStorage.userInfoData, jsonEncode(data['member']));

    await XinHeLocalStorage.set(XinHeStorage.userLoginStatus, true);


    //发送全局登录成功通知
    bus.emit(XinHeStorage.KdLoginSucNoti);
  }

  static Future loginOutAndDelUserInfoData() async {
    await xinHeUserData.delUserInfoAndAppInfo();
    //重置请求头
    Map<String, dynamic> heardMap = {
      'x-access-token' :  '',
    };
    await XinHeLocalStorage.set(XinHeStorage.userLoginStatus, false);
    //重置登录状态
    xinHeUserData.appLoginState.value = false;
    // await XinHeLocalStorage.set(XinHeStorage.userId, 0);
    //
    // xinHeUserData.userId.value = 0;

    //设置请求头token等
    httpRequestManager.setXinHeDioBaseOptionsParam(headers: heardMap);

  }

  static modalToLoginPage() {
    Get.bottomSheet(XinHeLoginPage(),persistent: false,isScrollControlled: true);

  }


}